package com.cykj.pos.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.pos.controller.api.PosV1TransactioNotice;
import com.cykj.pos.domain.*;
import com.cykj.pos.mapper.BizPosMachineMapper;
import com.cykj.pos.mapper.BizPosMachineStatusRecordsMapper;
import com.cykj.pos.mapper.BizSFTInfoMapper;
import com.cykj.pos.service.*;
import com.cykj.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author 许晓龙
 * @date 2024/5/21 11:15
 */
@Service
public class BizSFTInfoServiceImpl extends ServiceImpl<BizSFTInfoMapper, BizSFTInfo> implements IBizSFTService {

    @Resource
    private BizSFTInfoMapper bizSFTInfoMapper;
    @Resource
    private IBizPosMachineService posMachineService;
    @Resource
    private IBizMerchantService bizMerchantService;
    @Resource
    private BizPosMachineMapper bizPosMachineMapper;
    @Resource
    private IBizMerchantInfoService bizMerchantInfoService;
    @Resource
    private BizPosMachineStatusRecordsMapper bizPosMachineStatusRecordsMapper;
    @Resource
    private PosV1TransactioNotice posV2TransactioNotice;

    @Resource
    private IBizPosMachineService bizPosMachineService;
    @Resource
    private IBizFeesChargedService bizFeesChargedService;
    @Resource
    private IBizTransactionAmount bizTransactionAmount;
    @Resource
    private IPosTransactionNoticeService iPosTransactionNoticeService;
    @Resource
    private IBizMerchantService iBizMerchantService;
    @Resource
    private ISysUserService sysUserService;


    @Override
    public AjaxResult sftMerchantInfoBind(JSONObject data) {
        System.out.println("接收绑定通知" + data.toJSONString());
        String merchantId = data.getString("merchantId");// 盛付通商户号
        String promoterId = data.getString("promoterId");// 代理商商户号
        String merchantName = data.getString("merchantName");// 商户名称
        String sn = data.getString("sn");
        String terminalId = data.getString("terminalId");// 逻辑终端号
        String productNo = data.getString("productNo");// 机具政策编号
        Date bindDate = data.getDate("bindDate");// 绑定日期 ，时间戳
        Date createTime = data.getDate("createTime");// 商户入网时间, 时间戳
        String directPromoterId = data.getString("directPromoterId");// 直属代理商商户号
        String policyNo = data.getString("productNo");// 政策编号
        String merchantPhoneNoMask = data.getString("merchantPhoneNoMask");// 商户手机号掩码
        String idCardNoMask = data.getString("idCardNoMask");// 商户身份证号掩码
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //查询设备信息
        BizPosMachine machine = posMachineService.selectPosBySnCode(sn);
        BizChannel channelDataByChannel = bizMerchantInfoService.getChannelDataByChannel(machine.getChannel());
        BizMerchant merchantByMerchId = iBizMerchantService.selectBizMerchantBySN(sn);
        SysUser sysUser = sysUserService.selectUserById(merchantByMerchId.getUserId());
        BizMerchantInfo bizMerchantInfo = new BizMerchantInfo();
        bizMerchantInfo.setInfoCompleted("1");
        bizMerchantInfo.setAuditStatus("3");
        bizMerchantInfo.setPosCode(sn);
        bizMerchantInfo.setMerchantName(merchantName);
        bizMerchantInfo.setMerchantShortHand(merchantName);
        bizMerchantInfo.setLegalPersonName(merchantName);
        bizMerchantInfo.setMerchId(machine.getMerchId());
        bizMerchantInfo.setMerchantCode(merchantId);
        bizMerchantInfo.setRegisterTime(sdf.format(createTime));
        bizMerchantInfo.setWhetherBinding("1");
        bizMerchantInfo.setOnLine("0");
        bizMerchantInfo.setContactPhoneNo(merchantPhoneNoMask);
        bizMerchantInfo.setCertificateNo(idCardNoMask);
        bizMerchantInfo.setSn(terminalId);
        bizMerchantInfo.setDeptId(sysUser.getDeptId());
        bizMerchantInfo.setUserId(sysUser.getUserId());
        bizMerchantInfo.setChannel(machine.getChannel());
        bizMerchantInfo.setChannelName(channelDataByChannel.getChannelName());

        BizMerchantInfo merchantInfoBySn = bizMerchantInfoService.getMerchantInfoBySn(sn);

        if (merchantInfoBySn != null) {
            bizMerchantInfo.setId(bizMerchantInfo.getId());
        }
        //存到自己的库里
        boolean save = bizMerchantInfoService.saveOrUpdate(bizMerchantInfo);
        BizPosMachineStatusRecords bizPosMachineStatusRecords1 = bizPosMachineStatusRecordsMapper.selectBizPosMachineStatusRecordsBysnCode(sn);
        if (bizPosMachineStatusRecords1 == null) {
            BizPosMachineStatusRecords bizPosMachineStatusRecords = new BizPosMachineStatusRecords();
            bizPosMachineStatusRecords.setSnCode(sn);
            bizPosMachineStatusRecords.setMerchantId(merchantId);
            bizPosMachineStatusRecords.setTime(sdf.format(bindDate));
            bizPosMachineStatusRecords.setName(merchantName);
            bizPosMachineStatusRecords.setPhoneNo(merchantPhoneNoMask);
            //新增激活表信息
            bizPosMachineStatusRecordsMapper.insertBizPosMachineStatusRecords(bizPosMachineStatusRecords);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult sftUnBind(JSONObject dataJson) {
        System.out.println("接收解绑通知" + dataJson.toJSONString());
        String posSn = dataJson.getString("sn");
        bizMerchantInfoService.deleteBySN(posSn);
        //删除激活表信息
        bizPosMachineStatusRecordsMapper.deleteBizPosRecordsNew(posSn);
        //更新设备表激活状态
        bizPosMachineStatusRecordsMapper.updatePosMachine(posSn);
        //运行函数
        iBizMerchantService.generateAllPosMerchantRelatedProcedure();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult transactionNotification(SynchronizationDataDTO dataJson) {
        posV2TransactioNotice.transact(dataJson);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult sftServiceFee(JSONObject dataJson) {
        SynchronizationDataDTO sy = new SynchronizationDataDTO();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String posSn = dataJson.getString("sn");
        sy.setPos_sn(posSn);
        sy.setTrans_type(291);
        sy.setAmnount(Double.parseDouble(dataJson.getString("cashPledgeAmt")));
        // sy.setUuid(IdWorker.getIdStr());
        sy.setHost_date(sdf.format(new Date()));
        sy.setMerchant_no(dataJson.getString("merchantId"));
        sy.setHost_ret_code("00");
        sy.setChannel("15");
        sy.setOrder_no(IdWorker.getIdStr());
        BizPosMachine posMachine = bizPosMachineService.getPosMachineBySnCode(sy.getPos_sn());
        if (sy.getTrans_type() == 291 && !posMachine.getPosActivateStatus().equals("2")) {   // 扣除押金
            iPosTransactionNoticeService.deposit(sy);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult sftSimFee(JSONObject dataJson) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        System.out.println(dataJson.getString("merchantId"));
        //发放流量费
        SynchronizationDataDTO sy = new SynchronizationDataDTO();
        sy.setSim_amt(Integer.parseInt(dataJson.getString("chargeAmount")));
        sy.setAmnount(Double.parseDouble(dataJson.getString("chargeAmount")));
        sy.setPos_sn(dataJson.getString("sn"));
        sy.setHost_date(sdf.format(new Date()));
        sy.setMerchant_no(dataJson.getString("merchantId"));
        sy.setHost_ret_code("00");
        sy.setChannel("15");
        sy.setTrans_type(293);
        sy.setOrder_no(IdWorker.getIdStr());
        iPosTransactionNoticeService.simFee(sy);
        return AjaxResult.success();
    }
}
